import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {
	//201521123091
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		String address = scanner.nextLine();
		int port = scanner.nextInt();
		scanner.nextLine();
		//201521123091
		try {
			Socket socket = new Socket(address, port);
			//201521123091
			Thread t1 = new Thread() {
				public void run() {
					Scanner in = null;
					try {
						in = new Scanner(socket.getInputStream());
						while (in.hasNextLine()) {
							System.out.println(in.nextLine());
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						System.out.println("123");
						if (in != null) {
							in.close();
						}
					}
				}
			};
			t1.start();
			
			//201521123091
			Thread t2 = new Thread() {
				public void run() {
					PrintWriter printWriter = null;
					try {
						printWriter = new PrintWriter(socket.getOutputStream(), true);
						while (scanner.hasNextLine()) {
							printWriter.println(scanner.nextLine());
							
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} finally {
						System.out.println("456");
						if (printWriter != null) {
							printWriter.close();
						}
					}
					
				}
			};
			t2.start();
			try {
				t1.join();
				t2.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			socket.close();
			scanner.close();
			System.out.println("789");
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		

	}
}
